<extend name="template/base_index" />


<block name="area_header">

</block>

<block name="area_body">
	{:W('Menus/topbar')}
	<div class="admin-main container-fluid">
		{:W('Menus/left')}
		<div class="admin-main-content">
			{:W('Menus/breadcrumb')}
			<div class="table-responsive well">

				<!-- 标题栏 -->
				<div class="main-title">
					<h2>{:L('VIEW_DATA_BACKUP')}</h2>
				</div>
				<!-- /标题栏 -->

				<div class="btn-controls">
					<a id="export" class="btn btn-default" href="javascript:;" autocomplete="off">{:L('BTN_BACKUP')}</a>
					<a id="optimize" class="btn btn-default" href="{:U('optimize')}">{:L('BTN_TABLE_OPTIMIZE')}</a>
					<a id="repair" class="btn btn-default" href="{:U('repair')}">{:L('BTN_TABLE_REPAIR')}</a>
				</div>

				<!-- 应用列表 -->
				<form id="export-form" method="post" action="{:U('export')}">
					<table class="table table-striped table table-hover  table-condensed">
						<thead>
							<tr>
								<th width="48">
									<input class="check-all" checked="chedked" type="checkbox" value="">
								</th>
								<th>{:L('VIEW_TABLE_NAME')}</th>
								<th width="120">{:L('VIEW_DATA_LENGTH')}</th>
								<th width="120">{:L('VIEW_DATA_SIZE')}</th>
								<th width="160">{:L('VIEW_CREATE_TIME')}</th>
								<th width="160">{:L('VIEW_BACKUP_STATUS')}</th>
								<th width="160">{:L('OPERATOR')}</th>
							</tr>
						</thead>
						<tbody>
							<volist name="list" id="table">
								<tr>
									<td class="num">
										<input class="ids" checked="chedked" type="checkbox" name="tables[]" value="{$table.name}">
									</td>
									<td>{$table.name}</td>
									<td>{$table.rows}</td>
									<td>{$table.data_length|format_bytes}</td>
									<td>{$table.create_time}</td>
									<td class="info">{:L('STATUS_NO_BACKUP')}</td>
									<td class="action">
										<a class="btn btn-sm btn-primary ajax-get no-refresh" href="{:U('Database/optimize?tables='.$table['name'])}">{:L('BTN_TABLE_OPTIMIZE')}</a>&nbsp;
										<a class="btn btn-sm btn-primary ajax-get no-refresh" href="{:U('Database/repair?tables='.$table['name'])}">{:L('BTN_TABLE_REPAIR')}</a>
									</td>
								</tr>
							</volist>
						</tbody>
					</table>
				</form>
				<!-- /应用列表 -->

			</div>
		</div>

	</div>
</block>

<block name="area_footer">
	<script type="text/javascript">
		(function($) {
			var $form = $("#export-form"),
				$export = $("#export"),
				tables
			$optimize = $("#optimize"), $repair = $("#repair");

			$optimize.add($repair).click(function() {
				$.post(this.href, $form.serialize(), function(data) {
					if (data.status) {
						$.scojs_message(data.info, $.scojs_message.TYPE_OK);
					} else {
						$.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
					}

				}, "json");
				return false;
			});

			$export.click(function() {
				$export.parent().children().button("loading");
				$export.html("正在发送备份请求...");
				$.post(
					$form.attr("action"),
					$form.serialize(),
					function(data) {
						if (data.status) {
							tables = data.tables;
							$export.html(data.info + "开始备份，请不要关闭本页面！");
							backup(data.tab);
							window.onbeforeunload = function() {
								return "正在备份数据库，请不要关闭！"
							}
						} else {

							$.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
							$export.parent().children().button("reset");
							$export.html("立即备份");

						}
					},
					"json"
				);
				return false;
			});

			function backup(tab, status) {
				status && showmsg(tab.id, "开始备份...(0%)");
				$.get($form.attr("action"), tab, function(data) {
					if (data.status) {
						showmsg(tab.id, data.info);

						if (!$.isPlainObject(data.tab)) {
							$export.parent().children().button("reset");
							$export.html("备份完成，点击重新备份");
							window.onbeforeunload = function() {
								return null
							}
							return;
						}
						backup(data.tab, tab.id != data.tab.id);
					} else {
						$.scojs_message(data.info, $.scojs_message.TYPE_ERROR);
						$export.parent().children().button("reset");
						$export.html("立即备份");

					}
				}, "json");

			}

			function showmsg(id, msg) {
				$form.find("input[value=" + tables[id] + "]").closest("tr").find(".info").html(msg);
			}
		})(jQuery);
	</script>
</block>